package com.huiyujiaoyu.powertime.ui.find;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.huiyujiaoyu.base.BaseFragment;
import com.huiyujiaoyu.powertime.R;
import com.huiyujiaoyu.powertime.model.IndexItemModel;
import com.huiyujiaoyu.powertime.ui.home.ArticleDetailsActivity;
import com.huiyujiaoyu.powertime.ui.home.SearchResult1Activity;
import com.huiyujiaoyu.powertime.ui.home.ShiPinDetailActivity;
import com.huiyujiaoyu.powertime.ui.home.VoiceDetailActivity;
import com.huiyujiaoyu.util.Constant;
import com.huiyujiaoyu.util.GlideUtil;
import com.huiyujiaoyu.util.StringUtils;
import com.huiyujiaoyu.widget.LlkjToolBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.huiyujiaoyu.base.AppContext.datas;

/**
 * Created by hhwly on 2017-06-26.
 * 搜索结果界面
 */

public class SearchResultFragment extends BaseFragment {
    @BindView(R.id.toolbar)
    LlkjToolBar mToolbar;
    @BindView(R.id.recycle)
    RecyclerView mRecycle;
    @BindView(R.id.swipe)
    SwipeRefreshLayout mSwipe;
    private BaseQuickAdapter<IndexItemModel, BaseViewHolder> adapter;
    private List<IndexItemModel> data;
    private int layId,type;

    public static SearchResultFragment newInstance(int type) {
        Bundle args = new Bundle();
        SearchResultFragment fragment = new SearchResultFragment();
        args.putInt(Constant.DATA,type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.layout_titswirecy;
    }


    @Override
    protected void initView() {
        mSwipe.setEnabled(false);
        mToolbar.setVisibility(View.GONE);

    }

    @Override
    protected void initData() {
        data = new ArrayList<>();
        type = getArguments().getInt(Constant.DATA);
        layId = type==1?R.layout.search_more_group:R.layout.column_list_item;
        adapter = new BaseQuickAdapter<IndexItemModel, BaseViewHolder>(layId,data) {
            @Override
            protected void convert(BaseViewHolder helper, final IndexItemModel item) {
                if(type==1){
                    int position = helper.getAdapterPosition();
                    String header =item.header;
                    final View view = helper.getView(R.id.ll_video_more);
                    if (position != 0
                            && header.equals(data.get(position - 1).header)) {
                        helper.getView(R.id.ll_item_all).setVisibility(View.GONE);
                    } else {
                        helper.getView(R.id.ll_item_all).setVisibility(View.VISIBLE);
                        if(item.isOk){
                            view.setVisibility(View.INVISIBLE);
                        }else{
                            view.setVisibility(View.VISIBLE);
                        }
                    }
                    helper.setText(R.id.tv_item_text,item.title);
                    helper.setText(R.id.tv_item_title,item.header);
                    StringUtils.setVisibility(helper.itemView,!item.isNotShow);
                    view.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ((SearchResult1Activity) getActivity()).setOpen(item.header);
                             item.isOk = true;
                             adapter.notifyDataSetChanged();
                        }
                    });

                }else{
                    GlideUtil.loadRoundImg((ImageView) helper.getView(R.id.image), StringUtils.getFullUrl(item.cover));
                    helper.setText(R.id.tv_title,item.name);
                    helper.setText(R.id.tv_num,item.subscribers+"人已经订阅");
                }
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(type==1){
                            switch (item.content_type) {
                                case Constant.CONTENT_TYPE_WZ:
                                    ArticleDetailsActivity.launch(mContext,item.content_id+"");
                                    break;
                                case Constant.CONTENT_TYPE_YP:
                                    VoiceDetailActivity.launch(mContext,item.content_id+"");
                                    break;
                                case Constant.CONTENT_TYPE_SP:
                                    ShiPinDetailActivity.launch(mContext,item.content_id+"");
                                    break;
                            }
                        }else{

                        }
                    }
                });

            }
        };
        mRecycle.setLayoutManager(new LinearLayoutManager(mContext));
        mRecycle.setAdapter(adapter);
    }
        public void setData(List<IndexItemModel> datas){
            data.clear();
            this.data.addAll(datas);
            adapter.notifyDataSetChanged();
        }
}
